package org.example.design.strategy.impl;

import org.example.design.strategy.ICouponDiscount;

import java.math.BigDecimal;
import java.util.Map;

/**
 * @Classname MJCouponDiscount
 * @Description 满减优惠具体实现
 * @Date 2020/11/11 4:52 下午
 * @Created by xiangjd
 */
public class MJCouponDiscount implements ICouponDiscount<Map<String, String>> {

    /**
     * 满减计算
     * 1. 判断满足x元后-n元，否则不减
     * 2. 最低支付金额1元
     */
    @Override
    public BigDecimal disCount(Map<String, String> couponInfo, BigDecimal skuPrice) {
        //满减商品要求金额
        String x = couponInfo.get("x");
        //优惠具体金额
        String n = couponInfo.get("n");
        /**
         * 小于满减满减要求金额
         */
        if (skuPrice.compareTo(new BigDecimal(x)) < 0) {
            return skuPrice;
        }
        //减去优惠金额判断
        BigDecimal count = skuPrice.subtract(new BigDecimal(n));
        if (count.compareTo(BigDecimal.ZERO) < 1) {
            return BigDecimal.ONE;
        }
        return count;
    }
}
